<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="description" content="刘清政">
  <meta name="keyword" content="hexo-theme">
  
    <link rel="shortcut icon" href="/css/images/logo.png">
  
  <title>
    
      3-IO操作与IO模型 | Justin-刘清政的博客
    
  </title>
  <link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/tomorrow.min.css" rel="stylesheet">
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/css/plugins/gitment.css">

  
  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/geopattern/1.2.3/js/geopattern.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.js"></script>
  
    
<script src="/js/qrious.js"></script>

  
  
    
<script src="/js/gitment.js"></script>

  
  

  
<meta name="generator" content="Hexo 4.2.0"></head>
<div class="wechat-share">
  <img src="/css/images/logo.png" />
</div>

  <body>
    <header class="header fixed-header">
  <div class="header-container">
    <a class="home-link" href="/">
      <div class="logo"></div>
      <span>Justin-刘清政的博客</span>
    </a>
    <ul class="right-list">
      
        <li class="list-item">
          
            <a href="/" class="item-link">主页</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/tags/" class="item-link">标签</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/archives/" class="item-link">归档</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/about/" class="item-link">关于我</a>
          
        </li>
      
    </ul>
    <div class="menu">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </div>
    <div class="menu-mask">
      <ul class="menu-list">
        
          <li class="menu-item">
            
              <a href="/" class="menu-link">主页</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/tags/" class="menu-link">标签</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/archives/" class="menu-link">归档</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/about/" class="menu-link">关于我</a>
            
          </li>
        
      </ul>
    </div>
  </div>
</header>

    <div id="article-banner">
  <h2>3-IO操作与IO模型</h2>



  <p class="post-date">2020-03-25</p>
    <!-- 不蒜子统计 -->
    <span id="busuanzi_container_page_pv" style='display:none' class="">
        <i class="icon-smile icon"></i> 阅读数：<span id="busuanzi_value_page_pv"></span>次
    </span>
  <div class="arrow-down">
    <a href="javascript:;"></a>
  </div>
</div>
<main class="app-body flex-box">
  <!-- Article START -->
  <article class="post-article">
    <section class="markdown-content"><h2 id="一-IO操作本质"><a href="#一-IO操作本质" class="headerlink" title="一 IO操作本质"></a>一 IO操作本质</h2><p>数据复制的过程中不会消耗CPU</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 1 内存分为内核缓冲区和用户缓冲区</span></span><br><span class="line"><span class="comment"># 2 用户的应用程序不能直接操作内核缓冲区，需要将数据从内核拷贝到用户才能使用</span></span><br><span class="line"><span class="comment"># 3 而IO操作、网络请求加载到内存的数据一开始是放在内核缓冲区的</span></span><br></pre></td></tr></table></figure>

<img src="https://tva1.sinaimg.cn/large/00831rSTly1gd8rw1pqm5j31700m2tdf.jpg" alt="image-20200325231658991" style="zoom:50%;" />

<h2 id="二-IO模型"><a href="#二-IO模型" class="headerlink" title="二 IO模型"></a>二 IO模型</h2><h3 id="1-BIO-–-阻塞模式I-O"><a href="#1-BIO-–-阻塞模式I-O" class="headerlink" title="1. BIO – 阻塞模式I/O"></a><strong>1. BIO – 阻塞模式I/O</strong></h3><p>用户进程从发起请求，到最终拿到数据前，一直挂起等待； 数据会由用户进程完成拷贝</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">举个例子：一个人去 商店买一把菜刀，</span></span><br><span class="line"><span class="string">他到商店问老板有没有菜刀（发起系统调用）</span></span><br><span class="line"><span class="string">如果有（表示在内核缓冲区有需要的数据）</span></span><br><span class="line"><span class="string">老板直接把菜刀给买家（从内核缓冲区拷贝到用户缓冲区）</span></span><br><span class="line"><span class="string">这个过程买家一直在等待</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">如果没有，商店老板会向工厂下订单（IO操作，等待数据准备好）</span></span><br><span class="line"><span class="string">工厂把菜刀运给老板（进入到内核缓冲区）</span></span><br><span class="line"><span class="string">老板把菜刀给买家（从内核缓冲区拷贝到用户缓冲区）</span></span><br><span class="line"><span class="string">这个过程买家一直在等待</span></span><br><span class="line"><span class="string">是同步io</span></span><br><span class="line"><span class="string">'''</span></span><br></pre></td></tr></table></figure>

<img src="https://tva1.sinaimg.cn/large/00831rSTly1gd8rw5x47nj318s0nwq9b.jpg" alt="image-20200325231903075" style="zoom:50%;" />

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> socket</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">server = socket.socket()</span><br><span class="line">server.bind((<span class="string">'127.0.0.1'</span>,<span class="number">8080</span>))</span><br><span class="line">server.listen(<span class="number">5</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">    conn, addr = server.accept()</span><br><span class="line">    <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">        <span class="keyword">try</span>:</span><br><span class="line">            data = conn.recv(<span class="number">1024</span>)</span><br><span class="line">            <span class="keyword">if</span> len(data) == <span class="number">0</span>:<span class="keyword">break</span></span><br><span class="line">            print(data)</span><br><span class="line">            conn.send(data.upper())</span><br><span class="line">        <span class="keyword">except</span> ConnectionResetError <span class="keyword">as</span> e:</span><br><span class="line">            <span class="keyword">break</span></span><br><span class="line">    conn.close()</span><br><span class="line">    </span><br><span class="line"><span class="comment"># 在服务端开设多进程或者多线程 进程池线程池 其实还是没有解决IO问题	</span></span><br><span class="line">该等的地方还是得等 没有规避</span><br><span class="line">只不过多个人等待的彼此互不干扰</span><br></pre></td></tr></table></figure>



<h3 id="2-NIO-–-非阻塞模式I-O"><a href="#2-NIO-–-非阻塞模式I-O" class="headerlink" title="2. NIO – 非阻塞模式I/O"></a><strong>2. NIO – 非阻塞模式I/O</strong></h3><p>用户进程发起请求，如果数据没有准备好，那么立刻告知用户进程未准备好；此时用户进程可选择继续发起请求、或者先去做其他事情，稍后再回来继续发请求，直到被告知数据准备完毕，可以开始接收为止； 数据会由用户进程完成拷贝</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">举个例子：一个人去 商店买一把菜刀，</span></span><br><span class="line"><span class="string">他到商店问老板有没有菜刀（发起系统调用）</span></span><br><span class="line"><span class="string">老板说没有，在向工厂进货（返回状态）</span></span><br><span class="line"><span class="string">买家去别地方玩了会，又回来问，菜刀到了么（发起系统调用）</span></span><br><span class="line"><span class="string">老板说还没有（返回状态）</span></span><br><span class="line"><span class="string">买家又去玩了会（不断轮询）</span></span><br><span class="line"><span class="string">最后一次再问，菜刀有了（数据准备好了）</span></span><br><span class="line"><span class="string">老板把菜刀递给买家（从内核缓冲区拷贝到用户缓冲区）</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">整个过程轮询+等待：轮询时没有等待，可以做其他事，从内核缓冲区拷贝到用户缓冲区需要等待</span></span><br><span class="line"><span class="string">是同步io</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">同一个线程，同一时刻只能监听一个socket，造成浪费，引入io多路复用，同时监听读个socket</span></span><br><span class="line"><span class="string">'''</span></span><br></pre></td></tr></table></figure>



<img src="https://tva1.sinaimg.cn/large/00831rSTly1gd8rw9zwy9j317i0o6473.jpg" alt="image-20200325232410816" style="zoom:50%;" />

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="string">要自己实现一个非阻塞IO模型</span></span><br><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="keyword">import</span> socket</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">server = socket.socket()</span><br><span class="line">server.bind((<span class="string">'127.0.0.1'</span>, <span class="number">8081</span>))</span><br><span class="line">server.listen(<span class="number">5</span>)</span><br><span class="line">server.setblocking(<span class="literal">False</span>)</span><br><span class="line"><span class="comment"># 将所有的网络阻塞变为非阻塞</span></span><br><span class="line">r_list = []</span><br><span class="line">del_list = []</span><br><span class="line"><span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        conn, addr = server.accept()</span><br><span class="line">        r_list.append(conn)</span><br><span class="line">    <span class="keyword">except</span> BlockingIOError:</span><br><span class="line">        <span class="comment"># time.sleep(0.1)</span></span><br><span class="line">        <span class="comment"># print('列表的长度:',len(r_list))</span></span><br><span class="line">        <span class="comment"># print('做其他事')</span></span><br><span class="line">        <span class="keyword">for</span> conn <span class="keyword">in</span> r_list:</span><br><span class="line">            <span class="keyword">try</span>:</span><br><span class="line">                data = conn.recv(<span class="number">1024</span>)  <span class="comment"># 没有消息 报错</span></span><br><span class="line">                <span class="keyword">if</span> len(data) == <span class="number">0</span>:  <span class="comment"># 客户端断开链接</span></span><br><span class="line">                    conn.close()  <span class="comment"># 关闭conn</span></span><br><span class="line">                    <span class="comment"># 将无用的conn从r_list删除</span></span><br><span class="line">                    del_list.append(conn)</span><br><span class="line">                    <span class="keyword">continue</span></span><br><span class="line">                conn.send(data.upper())</span><br><span class="line">            <span class="keyword">except</span> BlockingIOError:</span><br><span class="line">                <span class="keyword">continue</span></span><br><span class="line">            <span class="keyword">except</span> ConnectionResetError:</span><br><span class="line">                conn.close()</span><br><span class="line">                del_list.append(conn)</span><br><span class="line">        <span class="comment"># 挥手无用的链接</span></span><br><span class="line">        <span class="keyword">for</span> conn <span class="keyword">in</span> del_list:</span><br><span class="line">            r_list.remove(conn)</span><br><span class="line">        del_list.clear()</span><br><span class="line"></span><br><span class="line"><span class="comment"># 客户端</span></span><br><span class="line"><span class="keyword">import</span> socket</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">client = socket.socket()</span><br><span class="line">client.connect((<span class="string">'127.0.0.1'</span>,<span class="number">8081</span>))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">    client.send(<span class="string">b'hello world'</span>)</span><br><span class="line">    data = client.recv(<span class="number">1024</span>)</span><br><span class="line">    print(data)</span><br></pre></td></tr></table></figure>



<h3 id="3-IO-Multiplexing-I-O多路复用模型"><a href="#3-IO-Multiplexing-I-O多路复用模型" class="headerlink" title="3. IO Multiplexing - I/O多路复用模型"></a><strong>3. IO Multiplexing - I/O多路复用模型</strong></h3><p>类似BIO，只不过找了一个代理，来挂起等待，并能同时监听多个请求； 数据会由用户进程完成拷贝</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">举个例子：多个人去 一个商店买菜刀，</span></span><br><span class="line"><span class="string">多个人给老板打电话，说我要买菜刀（发起系统调用）</span></span><br><span class="line"><span class="string">老板把每个人都记录下来（放到select中）</span></span><br><span class="line"><span class="string">老板去工厂进货（IO操作）</span></span><br><span class="line"><span class="string">有货了，再挨个通知买到的人，来取刀（通知/返回可读条件）</span></span><br><span class="line"><span class="string">买家来到商店等待，老板把到给买家（从内核缓冲区拷贝到用户缓冲区）</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">多路复用：老板可以同时接受很多请求（select模型最大1024个，epoll模型），</span></span><br><span class="line"><span class="string">但是老板把到给买家这个过程，还需要等待，</span></span><br><span class="line"><span class="string">是同步io</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">强调：</span></span><br><span class="line"><span class="string">​ 1. 如果处理的连接数不是很高的话，使用select/epoll的web server不一定比使用multi-threading + blocking IO的web server性能更好，可能延迟还更大。select/epoll的优势并不是对于单个连接能处理得更快，而是在于能处理更多的连接。</span></span><br><span class="line"><span class="string">​ 2. 在多路复用模型中，对于每一个socket，一般都设置成为non-blocking，但是，如上图所示，整个用户的process其实是一直被block的。只不过process是被select这个函数block，而不是被socket IO给block。</span></span><br><span class="line"><span class="string">​ 结论: select的优势在于可以处理多个连接，不适用于单个连接</span></span><br><span class="line"><span class="string">'''</span></span><br></pre></td></tr></table></figure>



<img src="https://tva1.sinaimg.cn/large/00831rSTly1gd8rwe9ouij31740nkwll.jpg" alt="image-20200325232430850" style="zoom:50%;" />

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="string">当监管的对象只有一个的时候 其实IO多路复用连阻塞IO都比比不上！！！</span></span><br><span class="line"><span class="string">但是IO多路复用可以一次性监管很多个对象</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">server = socket.socket()</span></span><br><span class="line"><span class="string">conn,addr = server.accept()</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">监管机制是操作系统本身就有的 如果你想要用该监管机制(select)</span></span><br><span class="line"><span class="string">需要你导入对应的select模块</span></span><br><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="keyword">import</span> socket</span><br><span class="line"><span class="keyword">import</span> select</span><br><span class="line"></span><br><span class="line">server = socket.socket()</span><br><span class="line">server.bind((<span class="string">'127.0.0.1'</span>,<span class="number">8080</span>))</span><br><span class="line">server.listen(<span class="number">5</span>)</span><br><span class="line">server.setblocking(<span class="literal">False</span>)</span><br><span class="line">read_list = [server]</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">    r_list, w_list, x_list = select.select(read_list, [], [])</span><br><span class="line">    <span class="string">"""</span></span><br><span class="line"><span class="string">    帮你监管</span></span><br><span class="line"><span class="string">    一旦有人来了 立刻给你返回对应的监管对象</span></span><br><span class="line"><span class="string">    """</span></span><br><span class="line">    <span class="comment"># print(res)  # ([&lt;socket.socket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 8080)&gt;], [], [])</span></span><br><span class="line">    <span class="comment"># print(server)</span></span><br><span class="line">    <span class="comment"># print(r_list)</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> r_list:  <span class="comment">#</span></span><br><span class="line">        <span class="string">"""针对不同的对象做不同的处理"""</span></span><br><span class="line">        <span class="keyword">if</span> i <span class="keyword">is</span> server:</span><br><span class="line">            conn, addr = i.accept()</span><br><span class="line">            <span class="comment"># 也应该添加到监管的队列中</span></span><br><span class="line">            read_list.append(conn)</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            res = i.recv(<span class="number">1024</span>)</span><br><span class="line">            <span class="keyword">if</span> len(res) == <span class="number">0</span>:</span><br><span class="line">                i.close()</span><br><span class="line">                <span class="comment"># 将无效的监管对象 移除</span></span><br><span class="line">                read_list.remove(i)</span><br><span class="line">                <span class="keyword">continue</span></span><br><span class="line">            print(res)</span><br><span class="line">            i.send(<span class="string">b'heiheiheiheihei'</span>)</span><br><span class="line"></span><br><span class="line"> <span class="comment"># 客户端</span></span><br><span class="line"><span class="keyword">import</span> socket</span><br><span class="line">client = socket.socket()</span><br><span class="line">client.connect((<span class="string">'127.0.0.1'</span>,<span class="number">8080</span>))</span><br><span class="line"><span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line"></span><br><span class="line">    client.send(<span class="string">b'hello world'</span>)</span><br><span class="line">    data = client.recv(<span class="number">1024</span>)</span><br><span class="line">    print(data)</span><br></pre></td></tr></table></figure>

<h4 id="3-1"><a href="#3-1" class="headerlink" title="3.1"></a>3.1</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br></pre></td><td class="code"><pre><span class="line">IO复用：为了解释这个名词，首先来理解下复用这个概念，复用也就是共用的意思，这样理解还是有些抽象，为此，咱们来理解下复用在通信领域的使用，在通信领域中为了充分利用网络连接的物理介质，往往在同一条网络链路上采用时分复用或频分复用的技术使其在同一链路上传输多路信号，到这里我们就基本上理解了复用的含义，即公用某个“介质”来尽可能多的做同一类(性质)的事，那IO复用的“介质”是什么呢？为此我们首先来看看服务器编程的模型，客户端发来的请求服务端会产生一个进程来对其进行服务，每当来一个客户请求就产生一个进程来服务，然而进程不可能无限制的产生，因此为了解决大量客户端访问的问题，引入了IO复用技术，即：一个进程可以同时对多个客户请求进行服务。也就是说IO复用的“介质”是进程(准确的说复用的是select和poll，因为进程也是靠调用select和poll来实现的)，复用一个进程(select和poll)来对多个IO进行服务，虽然客户端发来的IO是并发的但是IO所需的读写数据多数情况下是没有准备好的，因此就可以利用一个函数(select和poll)来监听IO所需的这些数据的状态，一旦IO有数据可以进行读写了，进程就来对这样的IO进行服务。</span><br><span class="line"></span><br><span class="line">  </span><br><span class="line"></span><br><span class="line">理解完IO复用后，我们在来看下实现IO复用中的三个API(select、poll和epoll)的区别和联系</span><br><span class="line"></span><br><span class="line">select，poll，epoll都是IO多路复用的机制，I/O多路复用就是通过一种机制，可以监视多个描述符，一旦某个描述符就绪（一般是读就绪或者写就绪），能够通知应用程序进行相应的读写操作。但select，poll，epoll本质上都是同步I/O，因为他们都需要在读写事件就绪后自己负责进行读写，也就是说这个读写过程是阻塞的，而异步I/O则无需自己负责进行读写，异步I/O的实现会负责把数据从内核拷贝到用户空间。三者的原型如下所示：</span><br><span class="line"></span><br><span class="line">int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);</span><br><span class="line"></span><br><span class="line">int poll(struct pollfd *fds, nfds_t nfds, int timeout);</span><br><span class="line"></span><br><span class="line">int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="number">1.</span>select的第一个参数nfds为fdset集合中最大描述符值加<span class="number">1</span>，fdset是一个位数组，其大小限制为__FD_SETSIZE（<span class="number">1024</span>），位数组的每一位代表其对应的描述符是否需要被检查。第二三四参数表示需要关注读、写、错误事件的文件描述符位数组，这些参数既是输入参数也是输出参数，可能会被内核修改用于标示哪些描述符上发生了关注的事件，所以每次调用select前都需要重新初始化fdset。timeout参数为超时时间，该结构会被内核修改，其值为超时剩余的时间。</span><br><span class="line"></span><br><span class="line"> select的调用步骤如下：</span><br><span class="line"></span><br><span class="line">（<span class="number">1</span>）使用copy_from_user从用户空间拷贝fdset到内核空间</span><br><span class="line"></span><br><span class="line">（<span class="number">2</span>）注册回调函数__pollwait</span><br><span class="line"></span><br><span class="line">（<span class="number">3</span>）遍历所有fd，调用其对应的poll方法（对于socket，这个poll方法是sock_poll，sock_poll根据情况会调用到tcp_poll,udp_poll或者datagram_poll）</span><br><span class="line"></span><br><span class="line">（<span class="number">4</span>）以tcp_poll为例，其核心实现就是__pollwait，也就是上面注册的回调函数。</span><br><span class="line"></span><br><span class="line">（5）__pollwait的主要工作就是把current（当前进程）挂到设备的等待队列中，不同的设备有不同的等待队列，对于tcp_poll 来说，其等待队列是sk-&gt;sk_sleep（注意把进程挂到等待队列中并不代表进程已经睡眠了）。在设备收到一条消息（网络设备）或填写完文件数 据（磁盘设备）后，会唤醒设备等待队列上睡眠的进程，这时current便被唤醒了。</span><br><span class="line"></span><br><span class="line">（<span class="number">6</span>）poll方法返回时会返回一个描述读写操作是否就绪的mask掩码，根据这个mask掩码给fd_set赋值。</span><br><span class="line"></span><br><span class="line">（<span class="number">7</span>）如果遍历完所有的fd，还没有返回一个可读写的mask掩码，则会调用schedule_timeout是调用select的进程（也就是 current）进入睡眠。当设备驱动发生自身资源可读写后，会唤醒其等待队列上睡眠的进程。如果超过一定的超时时间（schedule_timeout 指定），还是没人唤醒，则调用select的进程会重新被唤醒获得CPU，进而重新遍历fd，判断有没有就绪的fd。</span><br><span class="line"></span><br><span class="line">（<span class="number">8</span>）把fd_set从内核空间拷贝到用户空间。</span><br><span class="line"></span><br><span class="line">总结下select的几大缺点：</span><br><span class="line"></span><br><span class="line">（<span class="number">1</span>）每次调用select，都需要把fd集合从用户态拷贝到内核态，这个开销在fd很多时会很大</span><br><span class="line"></span><br><span class="line">（<span class="number">2</span>）同时每次调用select都需要在内核遍历传递进来的所有fd，这个开销在fd很多时也很大</span><br><span class="line"></span><br><span class="line">（<span class="number">3</span>）select支持的文件描述符数量太小了，默认是<span class="number">1024</span></span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"></span><br><span class="line"><span class="number">2</span>．  poll与select不同，通过一个pollfd数组向内核传递需要关注的事件，故没有描述符个数的限制，pollfd中的events字段和revents分别用于标示关注的事件和发生的事件，故pollfd数组只需要被初始化一次。</span><br><span class="line"></span><br><span class="line"> poll的实现机制与select类似，其对应内核中的sys_poll，只不过poll向内核传递pollfd数组，然后对pollfd中的每个描述符进行poll，相比处理fdset来说，poll效率更高。poll返回后，需要对pollfd中的每个元素检查其revents值，来得指事件是否发生。</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"></span><br><span class="line"><span class="number">3</span>．直到Linux2<span class="number">.6</span>才出现了由内核直接支持的实现方法，那就是epoll，被公认为Linux2<span class="number">.6</span>下性能最好的多路I/O就绪通知方法。epoll可以同时支持水平触发和边缘触发（Edge Triggered，只告诉进程哪些文件描述符刚刚变为就绪状态，它只说一遍，如果我们没有采取行动，那么它将不会再次告知，这种方式称为边缘触发），理论上边缘触发的性能要更高一些，但是代码实现相当复杂。epoll同样只告知那些就绪的文件描述符，而且当我们调用epoll_wait()获得就绪文件描述符时，返回的不是实际的描述符，而是一个代表就绪描述符数量的值，你只需要去epoll指定的一个数组中依次取得相应数量的文件描述符即可，这里也使用了内存映射（mmap）技术，这样便彻底省掉了这些文件描述符在系统调用时复制的开销。另一个本质的改进在于epoll采用基于事件的就绪通知方式。在select/poll中，进程只有在调用一定的方法后，内核才对所有监视的文件描述符进行扫描，而epoll事先通过epoll_ctl()来注册一个文件描述符，一旦基于某个文件描述符就绪时，内核会采用类似callback的回调机制，迅速激活这个文件描述符，当进程调用epoll_wait()时便得到通知。</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"></span><br><span class="line">epoll既然是对select和poll的改进，就应该能避免上述的三个缺点。那epoll都是怎么解决的呢？在此之前，我们先看一下epoll 和select和poll的调用接口上的不同，select和poll都只提供了一个函数——select或者poll函数。而epoll提供了三个函 数，epoll_create,epoll_ctl和epoll_wait，epoll_create是创建一个epoll句柄；epoll_ctl是注 册要监听的事件类型；epoll_wait则是等待事件的产生。</span><br><span class="line"></span><br><span class="line">　　对于第一个缺点，epoll的解决方案在epoll_ctl函数中。每次注册新的事件到epoll句柄中时（在epoll_ctl中指定 EPOLL_CTL_ADD），会把所有的fd拷贝进内核，而不是在epoll_wait的时候重复拷贝。epoll保证了每个fd在整个过程中只会拷贝 一次。</span><br><span class="line"></span><br><span class="line">　　对于第二个缺点，epoll的解决方案不像select或poll一样每次都把current轮流加入fd对应的设备等待队列中，而只在 epoll_ctl时把current挂一遍（这一遍必不可少）并为每个fd指定一个回调函数，当设备就绪，唤醒等待队列上的等待者时，就会调用这个回调 函数，而这个回调函数会把就绪的fd加入一个就绪链表）。epoll_wait的工作实际上就是在这个就绪链表中查看有没有就绪的fd（利用 schedule_timeout()实现睡一会，判断一会的效果，和select实现中的第<span class="number">7</span>步是类似的）。</span><br><span class="line"></span><br><span class="line">　　对于第三个缺点，epoll没有这个限制，它所支持的FD上限是最大可以打开文件的数目，这个数字一般远大于<span class="number">2048</span>,举个例子, 在<span class="number">1</span>GB内存的机器上大约是<span class="number">10</span>万左右，具体数目可以cat /proc/sys/fs/file-max察看,一般来说这个数目和系统内存关系很大。</span><br><span class="line"></span><br><span class="line">总结：</span><br><span class="line"></span><br><span class="line">（<span class="number">1</span>）select，poll实现需要自己不断轮询所有fd集合，直到设备就绪，期间可能要睡眠和唤醒多次交替。而epoll其实也需要调用 epoll_wait不断轮询就绪链表，期间也可能多次睡眠和唤醒交替，但是它是设备就绪时，调用回调函数，把就绪fd放入就绪链表中，并唤醒在 epoll_wait中进入睡眠的进程。虽然都要睡眠和交替，但是select和poll在“醒着”的时候要遍历整个fd集合，而epoll在“醒着”的 时候只要判断一下就绪链表是否为空就行了，这节省了大量的CPU时间，这就是回调机制带来的性能提升。</span><br><span class="line"></span><br><span class="line">（<span class="number">2</span>）select，poll每次调用都要把fd集合从用户态往内核态拷贝一次，并且要把current往设备等待队列中挂一次，而epoll只要 一次拷贝，而且把current往等待队列上挂也只挂一次（在epoll_wait的开始，注意这里的等待队列并不是设备等待队列，只是一个epoll内 部定义的等待队列），这也能节省不少的开销。</span><br><span class="line"><span class="comment">###############</span></span><br><span class="line">这三种IO多路复用模型在不同的平台有着不同的支持，而epoll在windows下就不支持，好在我们有selectors模块，帮我们默认选择当前平台下最合适的</span><br><span class="line"><span class="comment">##############</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#服务端</span></span><br><span class="line"><span class="keyword">from</span> socket <span class="keyword">import</span> *</span><br><span class="line"><span class="keyword">import</span> selectors</span><br><span class="line"></span><br><span class="line">sel=selectors.DefaultSelector()</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">accept</span><span class="params">(server_fileobj,mask)</span>:</span></span><br><span class="line">    conn,addr=server_fileobj.accept()</span><br><span class="line">    sel.register(conn,selectors.EVENT_READ,read)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">read</span><span class="params">(conn,mask)</span>:</span></span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        data=conn.recv(<span class="number">1024</span>)</span><br><span class="line">        <span class="keyword">if</span> <span class="keyword">not</span> data:</span><br><span class="line">            print(<span class="string">'closing'</span>,conn)</span><br><span class="line">            sel.unregister(conn)</span><br><span class="line">            conn.close()</span><br><span class="line">            <span class="keyword">return</span></span><br><span class="line">        conn.send(data.upper()+<span class="string">b'_SB'</span>)</span><br><span class="line">    <span class="keyword">except</span> Exception:</span><br><span class="line">        print(<span class="string">'closing'</span>, conn)</span><br><span class="line">        sel.unregister(conn)</span><br><span class="line">        conn.close()</span><br><span class="line"></span><br><span class="line">server_fileobj=socket(AF_INET,SOCK_STREAM)</span><br><span class="line">server_fileobj.setsockopt(SOL_SOCKET,SO_REUSEADDR,<span class="number">1</span>)</span><br><span class="line">server_fileobj.bind((<span class="string">'127.0.0.1'</span>,<span class="number">8088</span>))</span><br><span class="line">server_fileobj.listen(<span class="number">5</span>)</span><br><span class="line">server_fileobj.setblocking(<span class="literal">False</span>) <span class="comment">#设置socket的接口为非阻塞</span></span><br><span class="line">sel.register(server_fileobj,selectors.EVENT_READ,accept) <span class="comment">#相当于网select的读列表里append了一个文件句柄server_fileobj,并且绑定了一个回调函数accept</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">    events=sel.select() <span class="comment">#检测所有的fileobj，是否有完成wait data的</span></span><br><span class="line">    <span class="keyword">for</span> sel_obj,mask <span class="keyword">in</span> events:</span><br><span class="line">        callback=sel_obj.data <span class="comment">#callback=accpet</span></span><br><span class="line">        callback(sel_obj.fileobj,mask) <span class="comment">#accpet(server_fileobj,1)</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#客户端</span></span><br><span class="line"><span class="keyword">from</span> socket <span class="keyword">import</span> *</span><br><span class="line">c=socket(AF_INET,SOCK_STREAM)</span><br><span class="line">c.connect((<span class="string">'127.0.0.1'</span>,<span class="number">8088</span>))</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">    msg=input(<span class="string">'&gt;&gt;: '</span>)</span><br><span class="line">    <span class="keyword">if</span> <span class="keyword">not</span> msg:<span class="keyword">continue</span></span><br><span class="line">    c.send(msg.encode(<span class="string">'utf-8'</span>))</span><br><span class="line">    data=c.recv(<span class="number">1024</span>)</span><br><span class="line">    print(data.decode(<span class="string">'utf-8'</span>))</span><br></pre></td></tr></table></figure>



<h3 id="4-AIO-–-异步I-O模型"><a href="#4-AIO-–-异步I-O模型" class="headerlink" title="4. AIO – 异步I/O模型"></a><strong>4. AIO – 异步I/O模型</strong></h3><p>发起请求立刻得到回复，不用挂起等待； 数据会由内核进程主动完成拷贝</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">举个例子：还是买菜刀</span></span><br><span class="line"><span class="string">现在是网上下单到商店（系统调用）</span></span><br><span class="line"><span class="string">商店确认（返回）</span></span><br><span class="line"><span class="string">商店去进货（io操作）</span></span><br><span class="line"><span class="string">商店收到货把货发个卖家（从内核缓冲区拷贝到用户缓冲区）</span></span><br><span class="line"><span class="string">买家收到货（指定信号）</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">整个过程无等待</span></span><br><span class="line"><span class="string">异步io</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">AIO框架在windows下使用windows IOCP技术，在Linux下使用epoll多路复用IO技术模拟异步IO</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">市面上多数的高并发框架，都没有使用异步io而是用的io多路复用，因为io多路复用技术很成熟且稳定，并且在实际的使用过程中，异步io并没有比io多路复用性能提升很多，没有达到很明显的程度</span></span><br><span class="line"><span class="string">并且，真正的AIO编码难度比io多路复用高很多</span></span><br><span class="line"><span class="string">'''</span></span><br></pre></td></tr></table></figure>



<img src="https://tva1.sinaimg.cn/large/00831rSTly1gd8rwiz5grj318c0pgn3u.jpg" alt="image-20200325232454769" style="zoom:50%;" />

<h3 id="5-select-poll-和epoll"><a href="#5-select-poll-和epoll" class="headerlink" title="5 select poll 和epoll"></a>5 select poll 和epoll</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#  1 select poll 和epoll都是io多路复用技术</span></span><br><span class="line">select, poll , epoN都是io多路复用的机制。I/O多路复用就是通过一种机 制个进程可以监视多个描述符，一旦某个描述符就绪（一般是读就绪或者写就绪），能够通知程序进行相应的读写操作。但select, poll , epoll本质上都是同步I/O ,因为他们都需要在读写事件就绪后自己负责进行读写， 也就是说这个读写过程是阻塞的，而异步I/O则无需自己负责进行读写，异 步I/O的实现会负责把数据从内核拷贝到用户空间。</span><br><span class="line"></span><br><span class="line"><span class="comment"># 2 select</span></span><br><span class="line">select函数监视的文件描述符分<span class="number">3</span>类，分别是writefds、readfds、和 exceptfds。调用后select函数会阻塞，直到有描述副就绪（有数据可读、 可写、或者有<span class="keyword">except</span>）,或者超时（timeout指定等待时间，如果立即返回 设为null即可），函数返回。当select函数返回后，可以通过遍历fdset,来 找到就绪的描述符。</span><br><span class="line">select目前几乎在所有的平台上支持，其良好跨平台支持也是它的一个 优点。select的一个缺点在于单个进程能够监视的文件描述符的数量存在最大限制，在Linux上一般为<span class="number">1024</span> ,可以通过修改宏定义甚至重新编译内核的 方式提升这一限制，但是这样也会造成效率的降低。</span><br><span class="line"><span class="comment"># 3 poll</span></span><br><span class="line">不同于select使用三个位图来表示三个fdset的方式，poll使用一个 pollfd的指针实现。</span><br><span class="line">pollfd结构包含了要监视的event和发生的event,不再使用select <span class="string">'参数-值'</span>传递的方式。同时，pollfd并没有最大数量限制（但是数量过大后 性能也是会下降）。和select函数一样，poll返回后，需要轮询pollfd来获取就绪的描述符。</span><br><span class="line">从上面看，select和poll都需要在返回后，通过遍历文件描述符来获取 已经就绪的socket。事实上，同时连接的大量客户端在一时刻可能只有很少的处于就绪状态，因此随着监视的描述符数量的增长，其效率也会线性下降</span><br><span class="line"></span><br><span class="line"><span class="comment"># 4 epoll</span></span><br><span class="line">epoll是在linux2<span class="number">.6</span>内核中提出的，是之前的select和poll的增强版本。相对 于select和poll来说，epoll更加灵活，没有描述符限制。epoll使用一个文 件描述符管理多个描述符，将用户关系的文件描述符的事件存放到内核的一个事件表中，这样在用户空间和内核空间的copy只需一次。</span><br><span class="line"></span><br><span class="line"><span class="comment"># 5 更好的例子理解</span></span><br><span class="line">老师检查同学作业，一班<span class="number">50</span>个人，一个一个问，同学，作业写完了没？select，poll</span><br><span class="line">老师检查同学作业，一班<span class="number">50</span>个人，同学写完了主动举手告诉老师，老师去检查 epoll</span><br><span class="line"></span><br><span class="line"><span class="comment"># 6 总结</span></span><br><span class="line">在并发高的情况下，连接活跃度不高，epoll比select好，网站http的请求，连了就断掉</span><br><span class="line">并发性不高，同时连接很活跃，select比epoll好，websocket的连接，长连接，游戏开发</span><br></pre></td></tr></table></figure>



<h2 id="三-同步I-O与异步I-O"><a href="#三-同步I-O与异步I-O" class="headerlink" title="三 同步I/O与异步I/O"></a>三 同步I/O与异步I/O</h2><ul>
<li>同步I/O<ul>
<li>概念：导致请求进程阻塞的I/O操作，直到I/O操作任务完成</li>
<li>类型：BIO、NIO、IO Multiplexing</li>
</ul>
</li>
<li>异步I/O<ul>
<li>概念：不导致进程阻塞的I/O操作</li>
<li>类型：AIO</li>
</ul>
</li>
</ul>
<p>注意：</p>
<ul>
<li>同步I/O与异步I/O判断依据是，是否会导致用户进程阻塞</li>
<li>BIO中socket直接阻塞等待（用户进程主动等待，并在拷贝时也等待）</li>
<li>NIO中将数据从内核空间拷贝到用户空间时阻塞（用户进程主动询问，并在拷贝时等待）</li>
<li>IO Multiplexing中select等函数为阻塞、拷贝数据时也阻塞（用户进程主动等待，并在拷贝时也等待）</li>
<li>AIO中从始至终用户进程都没有阻塞（用户进程是被动的）</li>
</ul>
<h2 id="四-并发-并行-同步-异步-阻塞-非阻塞"><a href="#四-并发-并行-同步-异步-阻塞-非阻塞" class="headerlink" title="四 并发-并行-同步-异步-阻塞-非阻塞"></a>四 并发-并行-同步-异步-阻塞-非阻塞</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 1 并发</span></span><br><span class="line">并发是指一个时间段内，有几个程序在同一个cpu上执行，但是同一时刻，只有一个程序在cpu上运行</span><br><span class="line">跑步，鞋带开了，停下跑步，系鞋带</span><br><span class="line"><span class="comment"># 2 并行</span></span><br><span class="line">指任意时刻点上，有多个程序同时运行在多个cpu上</span><br><span class="line">跑步，边跑步边听音乐</span><br><span class="line"><span class="comment"># 3 同步：</span></span><br><span class="line">指代码调用io操作时，必须等待io操作完成才返回的调用方式</span><br><span class="line"><span class="comment"># 4 异步</span></span><br><span class="line">异步是指代码调用io操作时，不必等io操作完成就返回调用方式</span><br><span class="line"><span class="comment"># 6 阻塞</span></span><br><span class="line">指调用函数时候，当前线程别挂起</span><br><span class="line"><span class="comment"># 6 非阻塞</span></span><br><span class="line">指调用函数时候，当前线程不会被挂起，而是立即返回</span><br><span class="line"></span><br><span class="line"><span class="comment"># 区别：</span></span><br><span class="line">同步和异步是消息通讯的机制</span><br><span class="line">阻塞和非阻塞是函数调用机制</span><br></pre></td></tr></table></figure>



<h2 id="五-IO设计模式"><a href="#五-IO设计模式" class="headerlink" title="五 IO设计模式"></a>五 IO设计模式</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"> Reactor模式，基于同步I&#x2F;O实现</span><br><span class="line">- Proactor模式，基于异步I&#x2F;O实现</span><br></pre></td></tr></table></figure>

<p>Reactor模式通常采用IO多路复用机制进行具体实现</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">- kqueue、epoll、poll、select等机制</span><br></pre></td></tr></table></figure>

<p>Proactor模式通常采用OS Asynchronous IO(AIO)的异步机制进行实现</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">- 前提是对应操作系统支持AIO，比如支持异步IO的linux(不太成熟)、具备IOCP的windows server(非常成熟)</span><br></pre></td></tr></table></figure>

<p>Reactor模式和Proactor模式都是事件驱动，主要实现步骤：</p>
<ol>
<li>事件注册：将事件与事件处理器进行分离。将事件注册到事件循环中，将事件处理器单独管理起来，记录其与事件的对应关系。</li>
<li>事件监听：启动事件循环，一旦事件已经就绪/完成，就立刻通知事件处理器</li>
<li>事件分发：当收到事件就绪/完成的信号，便立刻激活与之对应的事件处理器</li>
<li>事件处理：在进程/线程/协程中执行事件处理器</li>
</ol>
<p>使用过程中，用户通常只负责<strong>定义事件和事件处理器</strong>并将其注册以及一开始的<strong>事件循环的启动</strong>，这个过程就会是以异步的形式执行任务。</p>
<h3 id="Reactor模式"><a href="#Reactor模式" class="headerlink" title="Reactor模式"></a>Reactor模式</h3><img src="https://tva1.sinaimg.cn/large/00831rSTly1gd8rwn55tej31620u0ti4.jpg" alt="image-20200325235039094" style="zoom:50%;" />

<h3 id="Proactor模式"><a href="#Proactor模式" class="headerlink" title="Proactor模式"></a>Proactor模式</h3><img src="https://tva1.sinaimg.cn/large/00831rSTly1gd8rwqdfvnj315x0u047e.jpg" alt="image-20200325235058486" style="zoom:50%;" />

<h3 id="对比分析"><a href="#对比分析" class="headerlink" title="对比分析"></a>对比分析</h3><p>Reactor模型处理耗时长的操作会造成事件分发的阻塞，影响到后续事件的处理；</p>
<p>Proactor模型实现逻辑复杂；依赖操作系统对异步的支持，目前实现了纯异步操作的操作系统少，实现优秀的如windows IOCP，但由于其windows系统用于服务器的局限性，目前应用范围较小；而Unix/Linux系统对纯异步的支持有限，因而应用事件驱动的主流还是基于select/epoll等实现的reactor模式</p>
<p>Python中：如asyncio、gevent、tornado、twisted等异步模块都是依据事件驱动模型设计，更多的都是使用reactor模型，其中部分也支持proactor模式，当然需要根据当前运行的操作系统环境来进行手动配置</p>
</section>
    <!-- Tags START -->
    
      <div class="tags">
        <span>Tags:</span>
        
  <a href="/tags#Python" >
    <span class="tag-code">Python</span>
  </a>

      </div>
    
    <!-- Tags END -->
    <!-- NAV START -->
    
  <div class="nav-container">
    <!-- reverse left and right to put prev and next in a more logic postition -->
    
      <a class="nav-left" href="/python/Python%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/1-%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E7%9A%84%E5%8F%91%E5%B1%95%E5%8F%B2/">
        <span class="nav-arrow">← </span>
        
          1-Python中的GIL
        
      </a>
    
    
      <a class="nav-right" href="/python/Python%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/14-%E4%BD%BF%E7%94%A8%E7%BA%BF%E7%A8%8B%E7%9A%84%E5%9C%BA%E6%99%AF/">
        
          1-Python中的GIL
        
        <span class="nav-arrow"> →</span>
      </a>
    
  </div>

    <!-- NAV END -->
    <!-- 打赏 START -->
    
      <div class="money-like">
        <div class="reward-btn">
          赏
          <span class="money-code">
            <span class="alipay-code">
              <div class="code-image"></div>
              <b>使用支付宝打赏</b>
            </span>
            <span class="wechat-code">
              <div class="code-image"></div>
              <b>使用微信打赏</b>
            </span>
          </span>
        </div>
        <p class="notice">点击上方按钮,请我喝杯咖啡！</p>
      </div>
    
    <!-- 打赏 END -->
    <!-- 二维码 START -->
    
      <div class="qrcode">
        <canvas id="share-qrcode"></canvas>
        <p class="notice">扫描二维码，分享此文章</p>
      </div>
    
    <!-- 二维码 END -->
    
      <!-- Gitment START -->
      <div id="comments"></div>
      <!-- Gitment END -->
    
  </article>
  <!-- Article END -->
  <!-- Catalog START -->
  
    <aside class="catalog-container">
  <div class="toc-main">
  <!-- 不蒜子统计 -->
    <strong class="toc-title">目录</strong>
    
      <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#一-IO操作本质"><span class="toc-nav-text">一 IO操作本质</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#二-IO模型"><span class="toc-nav-text">二 IO模型</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#1-BIO-–-阻塞模式I-O"><span class="toc-nav-text">1. BIO – 阻塞模式I&#x2F;O</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#2-NIO-–-非阻塞模式I-O"><span class="toc-nav-text">2. NIO – 非阻塞模式I&#x2F;O</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#3-IO-Multiplexing-I-O多路复用模型"><span class="toc-nav-text">3. IO Multiplexing - I&#x2F;O多路复用模型</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#3-1"><span class="toc-nav-text">3.1</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#4-AIO-–-异步I-O模型"><span class="toc-nav-text">4. AIO – 异步I&#x2F;O模型</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#5-select-poll-和epoll"><span class="toc-nav-text">5 select poll 和epoll</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#三-同步I-O与异步I-O"><span class="toc-nav-text">三 同步I&#x2F;O与异步I&#x2F;O</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#四-并发-并行-同步-异步-阻塞-非阻塞"><span class="toc-nav-text">四 并发-并行-同步-异步-阻塞-非阻塞</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#五-IO设计模式"><span class="toc-nav-text">五 IO设计模式</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#Reactor模式"><span class="toc-nav-text">Reactor模式</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#Proactor模式"><span class="toc-nav-text">Proactor模式</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#对比分析"><span class="toc-nav-text">对比分析</span></a></li></ol></li></ol>
    
  </div>
</aside>
  
  <!-- Catalog END -->
</main>

<script>
  (function () {
    var url = 'http://www.liuqingzheng.top/python/Python并发编程/25-IO模型/';
    var banner = ''
    if (banner !== '' && banner !== 'undefined' && banner !== 'null') {
      $('#article-banner').css({
        'background-image': 'url(' + banner + ')'
      })
    } else {
      $('#article-banner').geopattern(url)
    }
    $('.header').removeClass('fixed-header')

    // error image
    $(".markdown-content img").on('error', function() {
      $(this).attr('src', 'http://file.muyutech.com/error-img.png')
      $(this).css({
        'cursor': 'default'
      })
    })

    // zoom image
    $(".markdown-content img").on('click', function() {
      var src = $(this).attr('src')
      if (src !== 'http://file.muyutech.com/error-img.png') {
        var imageW = $(this).width()
        var imageH = $(this).height()

        var zoom = ($(window).width() * 0.95 / imageW).toFixed(2)
        zoom = zoom < 1 ? 1 : zoom
        zoom = zoom > 2 ? 2 : zoom
        var transY = (($(window).height() - imageH) / 2).toFixed(2)

        $('body').append('<div class="image-view-wrap"><div class="image-view-inner"><img src="'+ src +'" /></div></div>')
        $('.image-view-wrap').addClass('wrap-active')
        $('.image-view-wrap img').css({
          'width': `${imageW}`,
          'transform': `translate3d(0, ${transY}px, 0) scale3d(${zoom}, ${zoom}, 1)`
        })
        $('html').css('overflow', 'hidden')

        $('.image-view-wrap').on('click', function() {
          $(this).remove()
          $('html').attr('style', '')
        })
      }
    })
  })();
</script>


  <script>
    var qr = new QRious({
      element: document.getElementById('share-qrcode'),
      value: document.location.href
    });
  </script>



  <script>
    var gitmentConfig = "liuqingzheng";
    if (gitmentConfig !== 'undefined') {
      var gitment = new Gitment({
        id: "3-IO操作与IO模型",
        owner: "liuqingzheng",
        repo: "FuckBlog",
        oauth: {
          client_id: "32a4076431cf39d0ecea",
          client_secret: "94484bd79b3346a949acb2fda3c8a76ce16990c6"
        },
        theme: {
          render(state, instance) {
            const container = document.createElement('div')
            container.lang = "en-US"
            container.className = 'gitment-container gitment-root-container'
            container.appendChild(instance.renderHeader(state, instance))
            container.appendChild(instance.renderEditor(state, instance))
            container.appendChild(instance.renderComments(state, instance))
            container.appendChild(instance.renderFooter(state, instance))
            return container;
          }
        }
      })
      gitment.render(document.getElementById('comments'))
    }
  </script>




    <div class="scroll-top">
  <span class="arrow-icon"></span>
</div>
    <footer class="app-footer">
<!-- 不蒜子统计 -->
<span id="busuanzi_container_site_pv">
     本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
<span class="post-meta-divider">|</span>
<span id="busuanzi_container_site_uv" style='display:none'>
     本站访客数<span id="busuanzi_value_site_uv"></span>人
</span>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



  <p class="copyright">
    &copy; 2021 | Proudly powered by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank">小猿取经</a>
    <br>
    Theme by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank" rel="noopener">小猿取经</a>
  </p>
</footer>

<script>
  function async(u, c) {
    var d = document, t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
    s.parentNode.insertBefore(o, s);
  }
</script>
<script>
  async("//cdnjs.cloudflare.com/ajax/libs/fastclick/1.0.6/fastclick.min.js", function(){
    FastClick.attach(document.body);
  })
</script>

<script>
  var hasLine = 'true';
  async("//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js", function(){
    $('figure pre').each(function(i, block) {
      var figure = $(this).parents('figure');
      if (hasLine === 'false') {
        figure.find('.gutter').hide();
      }
      var lang = figure.attr('class').split(' ')[1] || 'code';
      var codeHtml = $(this).html();
      var codeTag = document.createElement('code');
      codeTag.className = lang;
      codeTag.innerHTML = codeHtml;
      $(this).attr('class', '').empty().html(codeTag);
      figure.attr('data-lang', lang.toUpperCase());
      hljs.highlightBlock(block);
    });
  })
</script>





<!-- Baidu Tongji -->

<script>
    var _baId = 'c5fd96eee1193585be191f318c3fa725';
    // Originial
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?" + _baId;
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
</script>


<script src="/js/script.js"></script>


<script src="/js/search.js"></script>


<script src="/js/load.js"></script>



  <span class="local-search local-search-google local-search-plugin" style="right: 50px;top: 70px;;position:absolute;z-index:2;">
      <input type="search" placeholder="站内搜索" id="local-search-input" class="local-search-input-cls" style="">
      <div id="local-search-result" class="local-search-result-cls"></div>
  </span>


  </body>
</html>